#import "BaseViewController.h"
#import "MBProgressHUD.h"
#import <MJExtension.h>

@interface BaseViewController ()<YoLoadingHelpDelegate>

@property (strong, nonatomic) MBProgressHUD *mb;

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = FFIVECOLOR;
    [self initLeftNavBtn];
    [self setUpSubViews];
}

#pragma mark - Nav

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:self.hideNaviBar animated:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.navigationController setNavigationBarHidden:self.hideNaviBar animated:YES];
}

///导航栏阴影
- (void)addNavigationShadow{
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    line.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:line];
    
    line.layer.shadowColor = HEXCOLOR(@"666666").CGColor;
    line.layer.shadowOffset = CGSizeMake(0,2);
    line.layer.shadowOpacity = 0.5;
    line.layer.shadowRadius = 1;
}

///导航栏阴影(特殊颜色)
- (void)addNavigationShadowWithColor:(UIColor *)color {
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    line.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:line];
    line.layer.shadowColor = color.CGColor;
    line.layer.shadowOffset = CGSizeMake(0,2);
    line.layer.shadowOpacity = 0.3;
    line.layer.shadowRadius = 2;
}

///导航栏下的分割线
- (void)addSpliteLine {
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    line.backgroundColor = HEXCOLOR(@"dedede");
    [self.view addSubview:line];
}

///设置左边的返回按钮
- (void)initLeftNavBtn {
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 44)];
    
    [leftBtn setImage:[UIImage imageNamed:@"back_black"] forState:(UIControlStateNormal)];
    leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -56, 0, 0);
    leftBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:(UIControlEventTouchUpInside)];
}


- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}


///导航栏右边的按钮
-(void)topMoreClick:(UIButton *)btn{
    [self topMoreClick];
}

///导航栏右边的按钮
-(void)topMoreClick{
    
}


///隐藏左边的按钮
-(void)hideLeftNavBtn{
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 44)];
    leftBtn.hidden = YES;
    [leftBtn setImage:[UIImage imageNamed:@""] forState:(UIControlStateNormal)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
}

- (void)pushVc:(UIViewController *)vc {
    if ([vc isKindOfClass:[UIViewController class]] == NO) return ;
    if (self.navigationController == nil) return ;
    if (vc.hidesBottomBarWhenPushed == NO) {
        vc.hidesBottomBarWhenPushed = YES;
    }
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)initRightNavBtnWithText:(NSString *)text{
    [self initRightNavBtnWithText:text font:15 textColor:[UIColor blackColor] edgn:(UIEdgeInsetsMake(0, 0, 0, 0))];
}

-(void)initRightNavBtnWithText:(NSString *)text font:(float)font{
    [self initRightNavBtnWithText:text font:font textColor:[UIColor blackColor] edgn:(UIEdgeInsetsMake(0, 0, 0, 0))];
}

-(void)initRightNavBtnWithText:(NSString *)text textColor:(UIColor *)textColor{
    [self initRightNavBtnWithText:text font:15 textColor:textColor edgn:(UIEdgeInsetsMake(0, 0, 0, 0))];
}

-(void)initRightNavBtnWithText:(NSString *)text edgn:(UIEdgeInsets)edgn{
    [self initRightNavBtnWithText:text font:15 textColor:[UIColor blackColor] edgn:edgn];
}

-(void)initRightNavBtnWithText:(NSString *)text font:(float)font textColor:(UIColor *)color edgn:(UIEdgeInsets)edgn{
    UIButton *right = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 44)];
    [right setTitle:text forState:(UIControlStateNormal)];
    [right setTitleColor:color forState:(UIControlStateNormal)];
    [right setTitleEdgeInsets:edgn];
    right.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    right.titleLabel.font = [UIFont systemFontOfSize:font];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:right];
    [right addTarget:self action:@selector(topMoreClick:) forControlEvents:(UIControlEventTouchUpInside)];
}

-(void)initRightNavBtnWithText:(NSString *)text font:(float)font textColor:(UIColor *)color edgn:(UIEdgeInsets)edgn wight:(UIFontWeight)wight{
    UIButton *right = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 44)];
    [right setTitle:text forState:(UIControlStateNormal)];
    [right setTitleColor:color forState:(UIControlStateNormal)];
    [right setTitleEdgeInsets:edgn];
    right.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    right.titleLabel.font = [UIFont systemFontOfSize:font weight:(UIFontWeightMedium)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:right];
    [right addTarget:self action:@selector(topMoreClick:) forControlEvents:(UIControlEventTouchUpInside)];
}


-(void)initRightNavBtnWithText:(NSString *)text font:(float)font textColor:(UIColor *)color backgroundColor:(UIColor *)bgColor frame:(CGRect) frame {
    UIButton *right = [[UIButton alloc]initWithFrame:frame];
    [right setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [right setTitle:text forState:(UIControlStateNormal)];
    [right setTitleColor:color forState:(UIControlStateNormal)];
    [right setBackgroundColor:bgColor];
    right.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    right.titleLabel.font = [UIFont systemFontOfSize:font];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:right];
    [right addTarget:self action:@selector(topMoreClick:) forControlEvents:(UIControlEventTouchUpInside)];
}


-(void)initRightNavBtnWithImg:(NSString *)imgName{
    [self initRightNavBtnWithImg:imgName edgn:(UIEdgeInsetsMake(0, 0, 0, 0))];
}

-(void)initRightNavBtnWithImg:(NSString *)imgName edgn:(UIEdgeInsets)edgn{
    UIButton *right = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 44)];
    [right setImage:[UIImage imageNamed:imgName] forState:(UIControlStateNormal)];
    [right setImageEdgeInsets:edgn];
    right.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:right];
    [right addTarget:self action:@selector(topMoreClick:) forControlEvents:(UIControlEventTouchUpInside)];
}


#pragma mark - MBProgressHUD

-(MBProgressHUD *)mb{
    if (!_mb) {
        _mb = [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:_mb];
    }
    return _mb;
}

-(void)mbShowWindowInit{
    if(!_mb){
        _mb = [[MBProgressHUD alloc]initWithView:[UIApplication sharedApplication].keyWindow];
        [[UIApplication sharedApplication].keyWindow addSubview:_mb];
    }
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:_mb];
}


-(void)mbShowLoading{
    [self mbShowLoadingText:@""];
}

-(void)mbShowLoadingText:(NSString *)text{
    [self mbShowLoadingText:text onWindow:NO];
}

-(void)mbShowLoadingText:(NSString *)text onWindow:(BOOL)window{
    if (window) {
        [self mbShowWindowInit];
    }else{
        [self mb];
    }
    _mb.label.text = nil;
    _mb.detailsLabel.text = text;
    _mb.detailsLabel.font = [UIFont systemFontOfSize:14];
    _mb.mode = MBProgressHUDModeIndeterminate;
    [_mb showAnimated:YES];
}


-(void)mbShowToast:(NSString *)text{
    [self mbShowToast:text onWindow:NO];
}

-(void)mbShowToast:(NSString *)text onWindow:(BOOL)window{
    if (window) {
        [self mbShowWindowInit];
        
    }else{
        [self mb];
    }
    
    _mb.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    _mb.bezelView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.8];
    
    _mb.detailsLabel.text = text;
    _mb.detailsLabel.font = [UIFont systemFontOfSize:14];
    _mb.mode = MBProgressHUDModeText;
    _mb.detailsLabel.textColor = [UIColor whiteColor];
    [_mb showAnimated:YES];
    [_mb hideAnimated:YES afterDelay:1.2];
}

-(void)mbShowError:(NSError *)error{
    NSString *info = @"";
    if ([error.userInfo.allKeys containsObject:@"NSLocalizedDescription"]) {
        info = [error.userInfo objectForKey:@"NSLocalizedDescription"];
    }else{
        info = error.domain;
    }
    if ([info isEqualToString:@"NSCocoaErrorDomain"]) {
        info = @"找不到服务器";
    }
    [self mbShowToast:info];
}


-(void)mbDismiss{
    [_mb hideAnimated:YES];
}


#pragma mark - loadingHelp
-(void)initYoLoadingHelp{
    [self initYoLoadingHelpWithFarme:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT) isShowLoading:YES];
}

-(void)initYoLoadingHelpWithSupreView:(UIView *)supView{
    [self initYoLoadingHelpWithFarme:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT) withSuperView:supView isShowLoading:YES];
}


-(void)initYoLoadingHelpWithFarme:(CGRect)frame isShowLoading:(BOOL)isShow{
    [self initYoLoadingHelpWithFarme:frame withSuperView:nil isShowLoading:isShow];
}


-(void)initYoLoadingHelpWithFarme:(CGRect)frame withSuperView:(UIView *)supView isShowLoading:(BOOL)isShow{
    _loadingHelp = [[YoLoadingHelpView alloc]initWithFrame:frame];
    self.loadingHelp.delegate = self;
    if (supView == nil) {
        [self.view addSubview:self.loadingHelp];
    }else{
        [supView addSubview:self.loadingHelp];
    }
    [self yoLoadingHelpHideAnima:NO];
    if (isShow) {
        [self yoShowLoading];
    }
}



-(void)yoShowLoading{
    //    [self.loadingHelp.layer removeAllAnimations];
    //    self.loadingHelp.alpha = 1;
    if (_loadingHelp == nil) {
        [self initYoLoadingHelp];
    }
    [self.loadingHelp showLoadingHelp];
}

-(void)yoShowError{
    //    NSString * info = @"";
    //    if ([error.userInfo.allKeys containsObject:@"NSLocalizedDescription"]) {
    //        info=[error.userInfo objectForKey:@"NSLocalizedDescription"];
    //    }else {
    //        info=error.domain;
    //    }
    //    if([info isEqualToString:@"NSCocoaErrorDomain"]){
    //        info = @"服务器开小岔了!";
    //    }
    //    [self.loadingHelp.layer removeAllAnimations];
    //    self.loadingHelp.alpha = 1;
    [self.loadingHelp showEmptyAndError:(LoadingHelpTypeNetWorkBtn)];
}

-(void)yoShowEmpty:(LoadingHelpType)type{
    if (!_loadingHelp) {
        _loadingHelp = [[YoLoadingHelpView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT)];
        self.loadingHelp.delegate = self;
        [self.view addSubview:self.loadingHelp];
    }
    [self.loadingHelp showEmptyAndError:type];
}

-(void)yoShowEmpty:(LoadingHelpType)type withSuperView:(UIView *)supView{
    if (!_loadingHelp) {
        _loadingHelp = [[YoLoadingHelpView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT)];
        self.loadingHelp.delegate = self;
        if (supView == nil) {
            [self.view addSubview:self.loadingHelp];
        }else{
            [supView addSubview:self.loadingHelp];
        }
    }
    [self.loadingHelp showEmptyAndError:type];
}


-(void)yoShowEmpty:(LoadingHelpType)type withFrame:(CGRect)frame{
    _loadingHelp.frame = frame;
    [self.loadingHelp showEmptyAndError:type];
}



///隐藏挡板
-(void)yoDismiss{
    [self yoLoadingHelpHideAnima:YES];
}



-(void)yoLoadingHelpHideAnima:(BOOL)anima{
    if (anima) {
        self.loadingHelp.hidden = YES;
        
        //        [UIView animateWithDuration:0.2 delay:0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        //            self.loadingHelp.alpha = 0;
        //        } completion:^(BOOL finished) {
        //
        //        }];
        //        return;
    }
    self.loadingHelp.hidden = YES;
}

// lodinghelp delegate

///loading 的错误帮助
-(void)loadingHelpError{
    [self yoLoadingErrorReload];
}

///点击按钮
-(void)loadingHelpBtnClick{
    [self yoEmptyClick];
}

///网络错误按钮，点击二次加载
-(void)yoLoadingErrorReload{
    [self yoShowLoading];
    [self httpLoadData];
}

///空界面按钮点击事件
-(void)yoEmptyClick{
    
}


-(void)autoError:(BOOL)netWork msg:(NSString *)msg{
    if (netWork) {
        [self yoShowError];
    }else{
        [self.loadingHelp showEmpty:msg withImg:@"error_network_no"];
    }
}


#pragma mark - 侧滑事件
//-(void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    [self OpenSilderBack];
//}

-(void)OpenSilderBack{
    ///开启侧滑返回
    self.closeSilder = NO;
    //    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    //        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    //        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    //    }
}

///禁用侧滑返回在viewDidAppear里面实现即可
-(void)CloseSilderBack{
    self.closeSilder = YES;
    //    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    //        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    //    }
}


-(void)didMoveToParentViewController:(UIViewController *)parent{
    [super didMoveToParentViewController:parent];
    if (!parent) {
        ///有问题，暂时不做操作（当一个vc下面嵌套多个子vc的时候，切换会走到这里来）
        //        [[HttpHelp shareInstance] cancelRequest];
    }
}


///关闭当前界面，跳转到下个界面
-(void)jumpViewControllerAndCloseSelf:(UIViewController *)vc{
    [self jumpViewControllerAndCloseSelf:vc index:1];
}

///关闭前index个界面，跳转到下个界面
-(void)jumpViewControllerAndCloseSelf:(UIViewController *)vc index:(int)index{
    NSArray *viewControlles = self.navigationController.viewControllers;
    NSMutableArray *newviewControlles = [NSMutableArray array];
    if ([viewControlles count] > 0) {
        for (int i=0; i < [viewControlles count] - index; i++) {
            [newviewControlles addObject:[viewControlles objectAtIndex:i]];
        }
    }
    [newviewControlles addObject:vc];
    [self.navigationController setViewControllers:newviewControlles animated:YES];
}



#pragma mark - getter and setter

//-(NSMutableArray *)dataMarr{
//    if (!_dataMarr) {
//        _dataMarr = [NSMutableArray array];
//    }
//    return _dataMarr;
//}


#pragma mark - 网络加载
-(void)httpLoadData{
    
}


#pragma mark - 其他数据解析方法
-(NSMutableArray *)dataAlaysis:(id)value withModel:(Class)model{
    NSMutableArray *arr = [NSMutableArray array];
    if ([value isKindOfClass:[NSArray class]]) {
        arr = [model mj_objectArrayWithKeyValuesArray:value];
        return arr;
    }
    if ([value isKindOfClass:[NSDictionary class]]) {
        id item = [model mj_objectWithKeyValues:value];
        if (item) {
            [arr addObject:model];
        }
    }
    NSLog(@"!!!!!!!!!!!需要解析的数据有问题！！！！！！！！");
    return nil;
}

#pragma mark - 析构函数

-(void)dealloc{
    NSLog(@"-----%@ dealloc -----", self.class);
}

#pragma mark - 子视图
///子视图
- (void)setUpSubViews{}

#pragma mark - lazy
- (YoLoadingHelpView *)emptyView{
    if (!_defaultEmptyView) {
        _defaultEmptyView = [[YoLoadingHelpView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [_defaultEmptyView yoShowEmpty];
    }
    return _defaultEmptyView;
}

@end
